import groovy.json.JsonSlurper
import spock.lang.Specification

import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger

/**
 * Created at 2017/11/7 18:21
 * @author gongweixin
 */
class JsonExample extends Specification {
    def "test jsonSlurper thread safe"() {
        JsonSlurper jsonSlurper = new JsonSlurper()
        def executer = Executors.newFixedThreadPool(20)
        AtomicInteger count = new AtomicInteger(0)
        for (def i in 1..100) {
            executer.execute {
                count.addAndGet(jsonSlurper.parseText('{"id":1}').id)
            }
        }
        while (executer.awaitTermination(1, TimeUnit.SECONDS)) {}

        expect:
        count.get() == 100
    }
}
